package states
{
	import org.flixel.*;
	
	import items.*;
	import treasures.*;
	import decorations.*;
	import enemies.*;
	import interactions.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class PlayState extends FlxState
	{
		protected var _edge:FlxTilemap;
		protected var _maze:Maze;
		protected var _decorations:FlxGroup;
		protected var _ui:UI;
		protected var _enemies:FlxGroup;
		protected var _interactions:FlxGroup;
		protected var _items:FlxGroup;
		protected var _treasures:FlxGroup;
		protected var _projectiles:FlxGroup;
		protected var _misc:FlxGroup;
		protected var _lights:FlxGroup;
		protected var _darkness:FlxSprite;
		protected var _animations:FlxGroup;
		
		override public function create():void
		{
			super.create();
			
			Registry.paused = false;
			
			Registry.addLayers();
			Registry.clearLayers();
			Registry.disableLayers();
			
			includeLayer(Registry.maze);
			includeLayer(Registry.ui);
			includeLayer(Registry.player);
			includeLayer(Registry.decorations);
			
			initVariables();
		}
		
		override public function destroy():void
		{
			Registry.removeLayers();
			Registry.clearLayers();
			
			super.destroy();
		}
		
		override public function update():void
		{
			super.update();
			
			//returnToMenu();
			
			FlxG.collide(_player, _maze);
			FlxG.collide(_enemies, _maze);
		}
		
		override public function draw():void
		{
			if (_darkness.exists)
			{
				_darkness.fill(Registry.darknessColor);
			}
			super.draw();
		}
		
		protected function get _player():Player
		{
			return Player.player;
		}
		
		protected function includeLayer(layer:FlxBasic):void
		{
			layer.exists = true;
		}
		
		protected function addEntity(e:Entity, randomLocation:Boolean = false, uniqueLocation:Boolean = false):Entity
		{
			
			if (randomLocation)
			{
				e.translateToRandomTile();
				if (uniqueLocation)
				{
					while (checkTileTakenByPoint(e.getMidpoint()) || e.atExit)
					{
						e.translateToRandomTile();
					}
				}
			}
			
			if (e is Item)
			{
				_items.add(e);
			}
			else if (e is Interaction)
			{
				_interactions.add(e);
			}
			else if (e is Enemy)
			{
				_enemies.add(e);
			}
			else if (e is Treasure)
			{
				_treasures.add(e);
			}
			else if (e is Decoration)
			{
				_decorations.add(e);
			}
			
			return e;
		}
		
		protected function overlapThreshold(object1:FlxObject, object2:FlxObject, threshold:Number = 15):Boolean
		{
			return FlxU.getDistance(object1.getMidpoint(), object2.getMidpoint()) < threshold;
		}
		
		protected function checkTileTakenByPoint(point:FlxPoint):Boolean
		{
			var tile:FlxPoint = Maze.getTileByPoint(point);
			return checkTileTakenByIndex(tile.x, tile.y);
		}
		
		protected function checkTileTakenByIndex(i:uint, j:uint):Boolean
		{
			var k:uint;
			var tile:FlxPoint;
			var e:Entity;
			
			for (k = 0; k < _enemies.length; k++)
			{
				e = _enemies.members[k];
				tile = Maze.getTileByPoint(e.getMidpoint());
				if (tile.x == i && tile.y == j)
				{
					return true;
				}
			}
			
			for (k = 0; k < _items.length; k++)
			{
				e = _items.members[k];
				tile = Maze.getTileByPoint(e.getMidpoint());
				if (tile.x == i && tile.y == j)
				{
					return true;
				}
			}
			
			for (k = 0; k < _interactions.length; k++)
			{
				e = _interactions.members[k];
				tile = Maze.getTileByPoint(e.getMidpoint());
				if (tile.x == i && tile.y == j)
				{
					return true;
				}
			}
			
			for (k = 0; k < _treasures.length; k++)
			{
				e = _treasures.members[k];
				tile = Maze.getTileByPoint(e.getMidpoint());
				if (tile.x == i && tile.y == j)
				{
					return true;
				}
			}
			
			return false;
		}
		
		private function initVariables():void
		{
			_maze = Registry.maze;
			_edge = Registry.edge;
			_ui = Registry.ui;
			_decorations = Registry.decorations;
			
			_projectiles = Registry.projectiles;
			_enemies = Registry.enemies;
			_interactions = Registry.interactions;
			_items = Registry.items;
			_treasures = Registry.treasures;
			_misc = Registry.misc;
			_lights = Registry.lights;
			_darkness = Registry.darkness;
			_animations = Registry.animations;
		}
		
		private function returnToMenu():void
		{
			if (FlxG.keys.justPressed("BACKSPACE"))
			{
				FlxG.switchState(new MenuState());
			}
		}
	}
}